package com.promotores;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class GestorParametro extends CreaTablas {
	CreaTablas db;	

	// Parametro table name
	private static final String TABLE_PARAMETROS = "Parametro";

	// TipoEvento Table Columns names	
	private static final String KEY_last_update = "last_update";
	private static final String KEY_ID = "id";
	
	public GestorParametro(Context context) {
		super(context);
		db = new CreaTablas(context);
	}
	
	// Adding new last_update
	public void addLastUpdate() {
		SQLiteDatabase db = this.getWritableDatabase();
		Date fecha = new Date();
		java.sql.Date fechaSQL = new java.sql.Date(fecha.getTime());
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		String strFecha = dateFormat.format(fechaSQL);
		strFecha = strFecha.substring(0, 10) + "T" + strFecha.substring(11,19) + "Z";
		ContentValues values = new ContentValues();
		values.put(KEY_ID, generarId());
		values.put(KEY_last_update, strFecha);
		// Inserting Row
		db.insert(TABLE_PARAMETROS, null, values);
		db.close(); // Closing database connection
	}
	
	public int updateLastUpdate() {
		SQLiteDatabase db = this.getWritableDatabase();
		Date fecha = new Date();
		java.sql.Date fechaSQL = new java.sql.Date(fecha.getTime());
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		String strFecha = dateFormat.format(fechaSQL);
		
		ContentValues values = new ContentValues();
		values.put(KEY_last_update, strFecha);

		// updating row
		return db.update(TABLE_PARAMETROS, values, KEY_last_update + " = ?",
				new String[] { strFecha });
	}
	
	public String getLastUpdate() {
		String last_upd_aux;
		SQLiteDatabase db = this.getReadableDatabase();
//		String selectQuery = "SELECT last_update FROM " + TABLE_PARAMETROS + " WHERE id = ( SELECT MAX(id) FROM " + TABLE_PARAMETROS + " ) ;";
		String selectQuery = "SELECT MAX(last_update) FROM " + TABLE_PARAMETROS;
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor != null)
			cursor.moveToFirst();
		else{
			cursor.close();
			return null;
		}
		last_upd_aux = cursor.getString(0); 
		cursor.close();
		db.close();
		return (last_upd_aux);
	}
	
	public int generarId(){
		SQLiteDatabase db = this.getReadableDatabase();
		String selectQuery = "SELECT MAX(id) FROM " + TABLE_PARAMETROS + ";";
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor != null)
			cursor.moveToFirst();	
		if (cursor.getString(0) == null)
			return 1;
		return Integer.parseInt((cursor.getString(0)))+1;
	}
	public int getTipoEventoCount() {
		String countQuery = "SELECT  COUNT(id) FROM " + TABLE_PARAMETROS;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		int aux = cursor.getCount();
		cursor.close();

		// return count
		db.close();
		
		return aux;
	}
	
}
